//
//  ZTGuideViewController.m
//  ZTExchange
//
//  Created by 锁飞 on 2018/11/8.
//  Copyright © 2018年 ZT. All rights reserved.
//

#import "ZTGuideViewController.h"


@interface ZTGuideImageView : UIView

@property (nonatomic, strong) UIImageView *imageViwe;
@property (nonatomic, strong) UILabel *largeTitleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, copy) NSString *largeTitle;
@property (nonatomic, copy) NSString *subTitle;

@end

@implementation ZTGuideImageView

- (instancetype)initWithImage:(UIImage *)image largeTile:(NSString *)largeTitle subTitle:(NSString *)subTitle {
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.image = image;
        self.largeTitle = largeTitle;
        self.subTitle = subTitle;
        [self initComments];
    }
    return self;
}

- (void)initComments {
    
    _imageViwe = [[UIImageView alloc] initWithImage:self.image];
    _largeTitleLabel = [self generateLabelWithFont:[UIFont systemFontOfSize:25] textColor:kMYHexToRGB(0x6b9cff, 1) textAligement:NSTextAlignmentCenter];
    _largeTitleLabel.text = self.largeTitle;
    _subTitleLabel = [self generateLabelWithFont:[UIFont systemFontOfSize:18] textColor:kMYHexToRGB(0x6b9cff, 1) textAligement:NSTextAlignmentCenter];
    _subTitleLabel.text = self.subTitle;
    
    [self addSubview:_imageViwe];
//    [self addSubview:_largeTitleLabel];
//    [self addSubview:_subTitleLabel];
    
    [_imageViwe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(kScreenW / (self.image.size.width / self.image.size.height));
    }];
    
//    [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).offset(20);
//        make.right.equalTo(self).offset(-20);
//        if (IPHONE_X) {
//            make.bottom.equalTo(self).offset(-(152 / 667.0 * kScreenH));
//        }
//        make.bottom.equalTo(self).offset(-(122 / 667.0 * kScreenH));
//    }];
//
//    [_largeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(_subTitleLabel);
//        make.bottom.equalTo(_subTitleLabel.mas_top).offset(-10);
//    }];
}

- (UILabel *)generateLabelWithFont:(UIFont *)font textColor:(UIColor *)color textAligement:(NSTextAlignment)textAligement {
    UILabel *label = [[UILabel alloc] init];
    label.font = font;
    label.textColor = color;
    label.textAlignment = textAligement;
    label.numberOfLines = 0;
    return label;
}

@end

const CGFloat skipButtonWith = 60;
const CGFloat skipButtonHeight = 25;

const CGFloat layerWith = 20;

static NSString * const versionKey = @"zt_currentVersion";

@interface ZTGuideViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIView *shapeLayerContentView;
@property (nonatomic, strong) UIButton *skipButton;
@property (nonatomic, strong) UIButton *nowButton;// 立即体验
@property (nonatomic, copy) NSArray *imageNames;
@property (nonatomic, copy) NSArray *largeTitles;
@property (nonatomic, copy) NSArray *subTitles;

@end

@implementation ZTGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initComments];
    [self startGuid];
}

- (void)initComments {
    _imageNames = @[@"firstGuide", @"secondGuide"];
//    _largeTitles = @[@"首创永续合约", @"抵押贷款", @"OTC"];
//    _subTitles = @[@"业内最人性化的合约设计", @"解决资金周转难题", @"不收手续费 不收广告费"];
}

- (void)startGuid {
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(kScreenW * _imageNames.count, 0);
    [self.view addSubview:_scrollView];
    
    for (int i = 0; i < _imageNames.count; ++i) {
        @autoreleasepool {
            ZTGuideImageView *guideView = [[ZTGuideImageView alloc] initWithImage:[UIImage imageNamed:_imageNames[i]] largeTile:_largeTitles[i] subTitle:_subTitles[i]];
            guideView.frame = CGRectMake(i * kScreenW, 0, kScreenW, kScreenH);
            [_scrollView addSubview:guideView];
        }
    }
    
    CGFloat skipBtnY = 25;
    if (IPHONE_X) {
        skipBtnY = 47;
    }
    _skipButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW - 20 - skipButtonWith, skipBtnY, skipButtonWith, skipButtonHeight)];
    [_skipButton setBackgroundColor:kMYHexToRGB(0x1b1951, 0.2)];
    [_skipButton setTitle:@"跳过" forState:UIControlStateNormal];
    [_skipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _skipButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _skipButton.layer.masksToBounds = YES;
    _skipButton.layer.cornerRadius = 12;
    [_skipButton addTarget:self action:@selector(clickButtonToMainVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_skipButton];
    
    
    _shapeLayerContentView = [[UIView alloc] init];
    CGFloat contentWith = layerWith * _imageNames.count;
    _shapeLayerContentView.frame = CGRectMake((kScreenW - contentWith) / 2, IPHONE_X ? (kScreenH - 24 - 70 - 2) : (kScreenH - 24 - 40 - 2), contentWith, 2);
    _shapeLayerContentView.backgroundColor = [kMYHexToRGB(0x73acff, 1) colorWithAlphaComponent:0.3];
    [self.view addSubview:_shapeLayerContentView];
    
    _shapeLayer = [CAShapeLayer layer];
    _shapeLayer.frame = CGRectMake(0, 0, layerWith, 2);
    _shapeLayer.backgroundColor = kMYHexToRGB(0x579cff, 1).CGColor;
    [_shapeLayerContentView.layer addSublayer:_shapeLayer];
    
    [self.nowButton setTitle:@"立刻体验" forState:UIControlStateNormal];
}

- (void)clickButtonToMainVC:(UIButton *)sender {
    kBLOCK_SAFE_EXEC(self.block);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat index = scrollView.contentOffset.x / kScreenW;
    self.shapeLayer.frame = CGRectMake(layerWith * index, 0, layerWith, 2);
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSInteger index = scrollView.contentOffset.x / kScreenW;
//    self.skipButton.hidden = [self isShowSkipButton:index];
//    self.shapeLayerContentView.hidden = [self isShowPageControl:index];
//    if ([self isShowNowButton]) {
//        [UIView animateWithDuration:0.25 animations:^{
//            self.nowButton.alpha = 1;
//            self.nowButton.superview.alpha = 1;
//        }];
//    } else {
//        [UIView animateWithDuration:0.25 animations:^{
//            self.nowButton.alpha = 0;
//            self.nowButton.superview.alpha = 0;
//        }];
//    }
//}

- (BOOL)isShowSkipButton:(NSInteger)index {
    if (index + 1 >= _imageNames.count) {
        return YES;
    }
    return NO;
}

- (BOOL)isShowNowButton {
    return _skipButton.isHidden;
}

- (BOOL)isShowPageControl:(NSInteger)index {
    return _skipButton.isHidden;
}

+ (BOOL)isShowGuideVC {

//    NSString *localVersion = [userDefaults stringForKey:versionKey];
//    NSString *currentVersion = kAppVersion;
//
//    BOOL isGreater = NO;
//    NSString *systemVersion = SYSTEM_VERSION;
//    if (systemVersion.length > 0 && systemVersion.doubleValue > 10.2) {
//        isGreater = [currentVersion compare:localVersion options:NSNumericSearch] == NSOrderedDescending ? YES : NO;
//    } else {
//        isGreater = [currentVersion isEqualToString:localVersion] == NO ? YES : NO;
//    }
//
//    if (localVersion == nil || isGreater == YES) {
//        //保存当前版本号
//        [self saveCurrentVersion];
//        return YES;
//    } else {
        return NO;
//    }
}

+ (void)saveCurrentVersion
{
    NSString *version = kAppVersion;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:version forKey:versionKey];
    [user synchronize];
}

- (UIButton *)nowButton {
    if (!_nowButton) {
        _nowButton = [UIButton new];
        _nowButton.layer.borderColor = [UIColor redColor].CGColor;
        _nowButton.layer.borderWidth = 0.5;
        _nowButton.layer.cornerRadius = 5;
        [_nowButton setTitle:@"立刻体验" forState:UIControlStateNormal];
        [_nowButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _nowButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_nowButton addTarget:self action:@selector(clickButtonToMainVC:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_nowButton];
        [_nowButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.width.mas_equalTo(90);
            make.height.mas_equalTo(24);
            if (IPHONE_X) {
                make.bottom.equalTo(self.view).offset(-(152 / 667.0 * kScreenH));
            }
            make.bottom.equalTo(self.view).offset(-(122 / 667.0 * kScreenH));
        }];
    }
    return _nowButton;
}

@end
